package LecoLi.Item01;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.demo.R;

import java.util.ArrayList;
import java.util.List;

import LecoLi.Util.Item01.viewPagerAdapter;

/**
 * Created by Le Co on 2017/5/16.
 */

public class ViewPagerFragment extends Fragment{
    private View view;

    private Button sendBut;

    private ViewPager viewPager;

    private LinearLayout linearLayout;

    private ImageView img1,img2,img3,img4,img5;

    private int viewPagerItemContent;

    private int [] imgs={R.drawable.icc_viewpage_01,R.drawable.icc_viewpage_02,R.drawable.icc_viewpage_03,R.drawable.icc_viewpage_04,R.drawable.icc_viewpage_05};

    private List<ImageView> imageViews;

    private int curPagerPosition=0,oldPagerPosition=0;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iniImage();
        iniDatas();
    }

    private void iniDatas() {

    }
    private void iniImage() {

    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        view = inflater.inflate(R.layout.leco_item_01,null);
        viewPager = $(R.id.leco_item_01_viewpager);
        linearLayout = $(R.id.leco_item_01_images);
        viewPager.setAdapter(new viewPagerAdapter(getActivity(),imgs));

        sendBut = $(R.id.leco_item_01_but);
        sendBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转
            }
        });

        img1 = $(R.id.leco_item_01_img_01);
        img2 = $(R.id.leco_item_01_img_02);
        img3 = $(R.id.leco_item_01_img_03);
        img4 = $(R.id.leco_item_01_img_04);
        img5 = $(R.id.leco_item_01_img_05);

        imageViews = new ArrayList<>();
        imageViews.add(img1);
        imageViews.add(img2);
        imageViews.add(img3);
        imageViews.add(img4);
        imageViews.add(img5);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                imageViews.get(oldPagerPosition).setBackgroundResource(R.drawable.icc_dian_hei);
                imageViews.get(position).setBackgroundResource(R.drawable.icc_dian_huang);
                oldPagerPosition=position;
                if (position==4){
                    sendBut.setVisibility(View.VISIBLE);
                }else{
                    sendBut.setVisibility(View.GONE);
                }

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        return view;
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    private void onCot(){

    }

    public <T extends View> T $(int i){
        return (T) view.findViewById(i);
    }

}
